package com.xiezc.util;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by xiezc on 17/1/16.
 */
public class ReqUtil {

    /**
     * 将req中的参数转换成具体的对象
     *
     * @param req
     * @param ct
     * @param <T>
     * @return
     */
    public static <T> T getObject(HttpServletRequest req, Class<T> ct) {
        try {
            //获得实体类对象
            T t = ct.newInstance();
            //获得实体类对象中的所有public方法
            Method[] methods = ct.getMethods();
            //遍历实体类中的方法
            for (Method method : methods) {
                //找到方法中的set方法
                String methodName = method.getName();
                if (methodName.startsWith("set")) {
                    //获得方法的所有参数类型数组
                    Class<?>[] parameterTypes = method.getParameterTypes();
                    if (parameterTypes.length != 1) {
                        continue;
                    }
                    //获得参数名称
                    String fieldName = methodName.substring(3);
                    char[] cs = fieldName.toCharArray();
                    cs[0] = Character.toLowerCase(cs[0]);
                    fieldName = new String(cs);
                    //从请求中获得参数的string值
                    String parameter = req.getParameter(fieldName);

                    if (parameter == null) {
                        continue;
                    }
                    //将String值转换成相应参数类型
                    Object parm = castParm(parameterTypes[0], parameter);
                    //调用set方法赋值
                    method.invoke(t, parm);
                }
            }
            return t;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将字符串转换成具体的参数类型
     *
     * @param clazz
     * @param parm
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    private static Object castParm(Class<?> clazz, String parm) throws IllegalAccessException, InstantiationException {
        String className = clazz.getName();
        if ("java.lang.Integer".equals(className) || "int".equals(className)) {
            return Integer.valueOf(parm);
        }
        if ("java.lang.Double".equals(className) || "double".equals(className)) {
            return Double.valueOf(parm);
        }
        if ("java.lang.Byte".equals(className) || "byte".equals(className)) {
            return Byte.valueOf(parm);
        }
        if ("java.lang.Float".equals(className) || "float".equals(className)) {
            return Float.valueOf(parm);
        }
        if ("java.lang.Boolean".equals(className) || "boolean".equals(className)) {
            return Boolean.valueOf(parm);
        }

        if ("java.lang.String".equals(className)) {
            return parm;
        }
        return new Object();
    }


}
